package com.wwt.blog.controller;

import com.wwt.blog.utils.QiniuUtils;
import com.wwt.blog.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UpLoadController {
    //上传图片
    @PostMapping
    public Result upload(@RequestParam("image")MultipartFile file){
        //获取上传图片的原始名字
        String filename = file.getOriginalFilename();
        //拼接出存储的最终名字
        String name = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(filename, ".");
        boolean upload = QiniuUtils.upload(file, name);
        if (upload)
            return Result.result_success(QiniuUtils.myUrl+name);
        else
            return Result.result_fail(2001,"文件上传失败");
    }
}
